# coding:utf-8
import sys
from typing import List
from flask import jsonify
from .init import celery
from .run_cases import RunCases

sys.path.append('../../')
celery.conf.update(imports='app.libs.tasks')


@celery.task()
def execute_debug_task(testcases: List[dict], _id=None):
    from app.models.case_group import CaseGroupModel
    group_id = testcases[0]['group_id']
    group_s = CaseGroupModel().query.filter_by(id=group_id).first_or_404()
    RunCases(group_s, testcases, group_id, group_s.space_id, _id).run_case()


@celery.task()
def execute_task(group_id):
    from app.models.case_group import CaseGroupModel
    from app.models.case import CaseModel
    group_s = CaseGroupModel().query.filter_by(id=group_id).first_or_404()
    # 获取用例列表
    testcases = jsonify(CaseModel.get_case(group_id)).json
    if not testcases:
        return
    RunCases(group_s, testcases, group_id, group_s.space_id).run_case()


def execute_debug_task1(testcases: List[dict], _id=None):
    from app.models.case_group import CaseGroupModel
    group_id = testcases[0]['group_id']
    # 拿数据 TODO
    group_s = CaseGroupModel().query.filter_by(id=group_id).first_or_404()
    RunCases(group_s, testcases, group_id, group_s.space_id, _id).run_case()
